
//
//  FileHelper.m
//  iOS_2D_RecordPath
//
//  Created by PC on 15/8/3.
//  Copyright (c) 2015年 FENGSHENG. All rights reserved.
//

#import "FileHelper.h"
#import "AMapRouteRecord.h"

@implementation FileHelper

+ (NSArray *)recordsArray
{
    NSString *path = [FileHelper baseDir];
    
    NSError *error = nil;
    NSArray *fileArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];
    return fileArray;
//    if (error!=nil)
//    {
//        return nil;
//    }
//    else
//    {
//        NSMutableArray *records = [[NSMutableArray alloc] init];
//        for (NSString *fileName in fileArray)
//        {
//            NSString *recordPath = [path stringByAppendingPathComponent:fileName] ;
//            recordPath = [recordPath stringByAppendingPathComponent:fileName] ;
//            
//            @try {
//                AMapRouteRecord *record = [NSKeyedUnarchiver unarchiveObjectWithFile:recordPath];
//                [records addObject:record];
//                
//            } @catch (NSException *exception) {
//                NSLog(@"exception :%@", exception);
//                [[NSFileManager defaultManager] removeItemAtPath:recordPath error:nil];
//                
//            } @finally {
//                
//            }
            
//        }
//        return [NSMutableArray arrayWithArray:records];
//    }
}

+ (NSString *)baseDir
{
//    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) objectAtIndex:0];

//    path = [path stringByAppendingPathComponent:@"NKMap"];
    NSString *path = [NSString stringWithFormat:@"%@/Library/NKMap", NSHomeDirectory()];
    
    BOOL pathSuccess = [[NSFileManager defaultManager] fileExistsAtPath:path];
    
    if (! pathSuccess)
    {
        pathSuccess = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    }

    
    return path;
}

// 先创建子目录，再创建filePathWithRecord，filePathWithImage，filePathWithAudio
+ (NSString *)filePathWithPath:(NSString *)pathName
{
    NSString *path = [[FileHelper baseDir] stringByAppendingPathComponent:pathName];
    
    BOOL pathSuccess = [[NSFileManager defaultManager] fileExistsAtPath:path];
    
    if (! pathSuccess)
    {
        pathSuccess = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    return path;
}


+ (NSString *)filePathWithFileName:(NSString *)pathName andFileName:(NSString *)fileName
{
    return [[FileHelper filePathWithPath:pathName] stringByAppendingPathComponent:fileName];
}

+ (NSString *)fileNameWithShortPath:(NSString *)longPath {
    // 截取NKMap之后
    NSRange range = [longPath rangeOfString: @"NKMap"];
    NSString *shortString = [longPath substringFromIndex:range.location+range.length+1];
    return shortString;
}

+ (BOOL)deleteFile:(NSString *)filename
{
    NSString *path = [FileHelper filePathWithPath:filename];
    
    NSError *error = nil;
    BOOL success = [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
    if (error != nil)
    {
        NSLog(@"%@",error);
    }
    
    return success;
}

+ (void)deletePath:(NSString *)path {

    [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}

+ (NSString *)creatFileNameWithDate {
    
    NSDate *nowDate = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    formatter.dateFormat = @"YYYY-MM-dd HH:mm:ss";
    return [formatter stringFromDate:nowDate];
    
}

@end
